package com.dotflat.fin.service.impl;

import com.dotflat.fin.PO.FinPlanPO;
import com.dotflat.fin.domain.FinPlan;
import com.dotflat.fin.domain.FinPlanOrder;
import com.dotflat.fin.repository.FinPlanRepository;
import com.dotflat.fin.repository.FinPlanOrderRepository;
import com.dotflat.fin.service.FinPlanService;
import com.dotflat.fin.service.FinPlanOrderService;
import com.dotflat.rest.PO.AreableObjectBasePO;
import com.dotflat.rest.service.impl.AreableObjectBaseServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 订单信息service
 *
 * @author ：Ron
 * @date ：Created in 2020/10/30 13:06
 */
@Service
public class FinPlanServiceImpl extends AreableObjectBaseServiceImpl<FinPlan, FinPlanRepository> implements FinPlanService {

    @Override
    @Autowired
    public void setBaseRepository(FinPlanRepository baseRepository) {
        super.setBaseRepository(baseRepository);
    }

    @Autowired
    FinPlanOrderRepository finPlanOrderRepository;

    @Autowired
    FinPlanOrderService finPlanOrderService;

    @Override
    public FinPlan notifyParents(String areaId, String staffId, String planId) {
        FinPlan plan = get(areaId, staffId, planId);

        List<FinPlanOrder> planOrders = finPlanOrderRepository.findAllByAreaIdAndPlanId(areaId, planId);

        for (FinPlanOrder finPlanOrder : planOrders){
            finPlanOrderService.notifyParent(areaId, finPlanOrder);
        }

        //TODO, notify parent;
        if (planOrders != null && planOrders.size() > 0) {
            plan.setNotifyCount(planOrders.size() + 0L);
            baseRepository.save(plan);
        }

        return plan;
    }

    @Override
    public FinPlan cancelPlan(String areaId, String staffId, String planId) {
        FinPlan plan = get(areaId, staffId, planId);

        plan.setCancelled(true);

        baseRepository.save(plan);

        return plan;
    }

    @Override
    public FinPlan updateFinPlanCount(FinPlan finPlan, boolean orderCount, boolean paiedCount){
        if (finPlan == null){
            return null;
        }

        if (orderCount){
            finPlan.setOrderCount(finPlanOrderRepository.countAllByPlanId(finPlan.getId()));
        }

        if (paiedCount){
            finPlan.setPaiedOrderCount(finPlanOrderRepository.countAllByPlanIdAndPayStatus(finPlan.getId(), 1));
        }

        if (paiedCount || orderCount){
            baseRepository.save(finPlan);
        }

        return finPlan;
    }

    public FinPlan updateFinPlanCount(String finPlanId, boolean orderCount, boolean paiedCount){
        FinPlan plan = baseRepository.getOne(finPlanId);
        return updateFinPlanCount(plan, orderCount,paiedCount);
    }

    @Override
    public FinPlan add(String areaId, String staffId, AreableObjectBasePO param) {
        if (param instanceof FinPlanPO){

            //判断这个收费计划里面产品是不是一样的。


            return add(areaId, staffId, param, o->{
                if (StringUtils.isEmpty(((FinPlanPO) param).getOrderSnBase())){
                    o.setOrderSnBase(System.currentTimeMillis() + "");
                }
            });
        }else{
            return super.add(areaId, staffId, param, null);
        }
    }

}
